<template>
  <div>
    <Activity
      :productValue="productValue"
      @getProductValue="getProductValue"
      @closeDrawer="closeDrawer"
      @refreshPage="emit('refreshData')"
      @prev="prev"
      @next="next"
      :isLoad="isLoading"
    />
    <!-- 详细资料 -->
    <System v-if="selectTag == 0" :productValue="productValue" />

    <!-- 操作日志 -->
    <Log
      v-if="selectTag == 1"
      :clueValue="productValue"
      :id="productValue.id"
      :bizType="BizTypeEnum.CRM_PRODUCT"
    />
  </div>
</template>

<script setup>
import { BizTypeEnum } from '@/api/crm/permission'
import Activity from './activity.vue'
import { useUserStoreWithOut } from '@/store/modules/user'
import Log from '@/views/crm/components/log/index.vue'
import System from './system.vue'
import * as ProductApi from '@/api/crm/product'

const isLoading = ref(false)
const productValue = ref({})
const productId = ref('')
const props = defineProps({
  selectTag: Number,
  id: String,
  nowIds: Array
})
const emit = defineEmits(['setDrawer', 'refreshData'])

const getProductValue = async () => {
  try {
    isLoading.value = true
    const res = await ProductApi.getProduct(productId.value)
    productValue.value = res
  } finally {
    isLoading.value = false
  }
}
/**上一条*/
const prev = () => {
  const index = props.nowIds.indexOf(productId.value) - 1
  if (props.nowIds[index]) {
    productId.value = props.nowIds[index]
    getProductValue()
  } else {
    ElMessage.error('没有更多了')
  }
}
/**下一条*/
const next = () => {
  const index = props.nowIds.indexOf(productId.value) + 1
  if (props.nowIds[index]) {
    productId.value = props.nowIds[index]
    getProductValue()
  } else {
    ElMessage.error('没有更多了')
  }
}
watch(
  () => props.id,
  (val) => {
    if (val) {
      productId.value = val
      getProductValue()
    }
  },
  { deep: true, immediate: true }
)
/**关闭*/
const closeDrawer = () => {
  emit('setDrawer')
}
</script>

<style lang="scss" scoped></style>
